package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.CurrentTreasurePresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.SpApplication;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CurrentTreasureBean;
import com.sskj.lib.bean.CurrentTypeBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 活期宝
 *
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.CURRENT_TREASURE)
public class CurrentTreasureActivity extends BaseActivity<CurrentTreasurePresenter> {

    @BindView(R2.id.functions_btc_rl)
    RelativeLayout functionsBtcRl;
    @BindView(R2.id.functions_btc_tv)
    TextView functionsBtcTv;
    @BindView(R2.id.functions_zhuanru_bt)
    Button functionsZhuanruBt;
    @BindView(R2.id.functions_zhuanchu_bt)
    Button functionsZhuanchuBt;
    @BindView(R2.id.functions_xianshi_iv)
    ImageView functionsXianshiIv;
    @BindView(R2.id.functions_zongzichan_tv)
    TextView functionsZongzichanTv;
    @BindView(R2.id.functions_zuori_tv)
    TextView functionsZuoriTv;
    @BindView(R2.id.functions_zrsy_tv)
    TextView functionsZrsyTv;
    @BindView(R2.id.functions_ljsy_tv)
    TextView functionsLjsyTv;
    @BindView(R2.id.functions_cyl_tv)
    TextView functionsCylTv;
    @BindView(R2.id.functions_nhsy_tv)
    TextView functionsNhsyTv;
    @BindView(R2.id.functions_btc_iv)
    ImageView functionsBtcIv;
    @BindView(R2.id.tvZrsy)
    TextView tvZrsy;
    @BindView(R2.id.tvLjsy)
    TextView tvLjsy;
    @BindView(R2.id.tvCyl)
    TextView tvCyl;
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.current_record_iv)
    LinearLayout currentRecordIv;
    private boolean isShow = true;
    List<String> coins = new ArrayList();
    private CurrentTreasureBean data;
    private CurrentTypeBean type;
    private String languages = "1";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_current_treasure;
    }

    @Override
    public CurrentTreasurePresenter getPresenter() {
        return new CurrentTreasurePresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "2";
        } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "1";
        } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "3";
        }
        mPresenter.getCoinList();
        updateCoinMsg("AICASH");
    }

    @Override
    public void initData() {
        //转入
        ClickUtil.click(300, functionsZhuanruBt, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_SHIFT)
                    .withString(Constans.CURRENEY, functionsBtcTv.getText().toString())
                    .withString(Constans.TYPE, Constans.ZHUANRU)
                    .navigation();
        });
        //转出
        ClickUtil.click(300, functionsZhuanchuBt, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_SHIFT)
                    .withString(Constans.CURRENEY, functionsBtcTv.getText().toString())
                    .withString(Constans.TYPE, Constans.ZHUANCHU)
                    .navigation();
        });
        //资金明细
        ClickUtil.click(300, currentRecordIv, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_CURRENT_RECORD).navigation();
        });
        //是否显示数据
        ClickUtil.click(300, functionsXianshiIv, () -> {
            if (isShow) {
                functionsZongzichanTv.setText("******");
                functionsZuoriTv.setText("******CNY");
                functionsZrsyTv.setText("******");
                functionsLjsyTv.setText("******");
                functionsCylTv.setText("******");
                functionsNhsyTv.setText("******%");
                isShow = false;
            } else {
                if (data != null) {
                    functionsZongzichanTv.setText(NumberUtil.keep2(data.getTot_price()));
                    functionsZuoriTv.setText(NumberUtil.keep2(ArithUtil.mul(Double.valueOf(data.getTaday_price()), data.getExchange_rate())) + "CNY");
                    functionsZrsyTv.setText(CommonUtil.dealCoinNum(data.getTaday_price(), getText(functionsBtcTv)));
                    functionsLjsyTv.setText(CommonUtil.dealCoinNum(data.getTot_profit_price(), getText(functionsBtcTv)));
                    functionsCylTv.setText(CommonUtil.dealCoinNum(data.getHold_price(), getText(functionsBtcTv)));
                    functionsNhsyTv.setText(data.getProfit() + "%");
                    isShow = true;
                } else {
                    functionsZongzichanTv.setText("0.00");
                    functionsZuoriTv.setText("0.00" + "CNY");
                    functionsZrsyTv.setText("0.00");
                    functionsLjsyTv.setText("0.00");
                    functionsCylTv.setText("0.00");
                    functionsNhsyTv.setText("0.00" + "%");
                }
            }
        });

        ClickUtil.click(300, functionsBtcRl, () -> {
            if (coins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.walletFunctions_currentTreasureBizhong));
                return;
            }
            BottomDialog.chooseCoins(this, coins, (tv) -> {
                functionsBtcTv.setText(tv);
                updateCoinMsg(tv);
            });
        });
    }

    private void updateCoinMsg(String coinName) {
        if (coinName.equals("AICASH")) {
            ImageUtil.setImage(R.mipmap.icon_ai, functionsBtcIv);
            tvZrsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity1));
            tvCyl.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity2));
            tvLjsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity3));
        }
        if (coinName.equals("BTC")) {
            ImageUtil.setImage(R.mipmap.icon_btc, functionsBtcIv);
            tvZrsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity40));
            tvCyl.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity50));
            tvLjsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity60));
        }
        if (coinName.equals("ETH")) {
            ImageUtil.setImage(R.mipmap.icon_eth, functionsBtcIv);
            tvZrsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity7));
            tvCyl.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity8));
            tvLjsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity9));
        }
        if (coinName.equals("LTC")) {
            ImageUtil.setImage(R.mipmap.icon_ltc, functionsBtcIv);
            tvZrsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity10));
            tvCyl.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity11));
            tvLjsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity12));
        }
        if (coinName.equals("USDT")) {
            ImageUtil.setImage(R.mipmap.icon_usdt, functionsBtcIv);
            tvZrsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity13));
            tvCyl.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity14));
            tvLjsy.setText(App.INSTANCE.getString(R.string.functions_currentTreasureActivity15));
        }
        mPresenter.getCurrentTreasureData(coinName, languages);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CurrentTreasureActivity.class);
        context.startActivity(intent);
    }


    public void getCurrentTreasureData(CurrentTreasureBean data) {
        this.data = data;
        if (data != null) {
            setData(data);
        }
    }

    /**
     * 获取活期宝数据并赋值
     */
    @SuppressLint("SetTextI18n")
    private void setData(CurrentTreasureBean data) {
        if (data == null) {
            return;
        }
        functionsZongzichanTv.setText(NumberUtil.keep2(data.getTot_price()));
        functionsZuoriTv.setText(NumberUtil.keep2(ArithUtil.mul(Double.valueOf(data.getTaday_price()), data.getExchange_rate())) + "CNY");
        functionsZrsyTv.setText(CommonUtil.dealCoinNum(data.getTaday_price(), getText(functionsBtcTv)));
        functionsLjsyTv.setText(CommonUtil.dealCoinNum(data.getTot_profit_price(), getText(functionsBtcTv)));
        functionsCylTv.setText(CommonUtil.dealCoinNum(data.getHold_price(), getText(functionsBtcTv)));
        functionsNhsyTv.setText(data.getProfit() + "%");
        RichText.from(data.getContent()).into(tvContent);
    }

    public void getCoinListOK(List<String> data) {
        if (data == null) {
            return;
        }
        coins.addAll(data);
    }
}
